package com.wyl.clinic.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.User;
import com.wyl.clinic.enums.RoleEnum;
import com.wyl.clinic.service.DoctorService;
import com.wyl.clinic.service.MenuService;
import com.wyl.clinic.service.UserService;
import com.wyl.clinic.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页列表
     * @param query
     * @return
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String roleId = (String)param.get("roleId");

        Page<User> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(User::getName,name);
        }
        if(StringUtils.isNotBlank(roleId)){
            lambdaQueryWrapper.eq(User::getRoleId,roleId);
        }

        IPage<User> result = userService.page(page,lambdaQueryWrapper);

        return Result.suc(result.getRecords(),result.getTotal());
    }

    /**
     * 根据账号查用户
     * @param account
     * @return
     */
    @GetMapping("/findByAc")
    public Result findByAc(@RequestParam String account){
        User user = userService.lambdaQuery().eq(User::getAccount, account).one();
        return ObjectUtil.isNotEmpty(user) ? Result.suc(user) : Result.fail();
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        //密码加密
        user.setPassword(Md5Util.md5Encryp(user.getPassword()));
        return userService.save(user)?Result.suc():Result.fail();
    }

    /**
     * 更新
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        //密码加密
        user.setPassword(Md5Util.md5Encryp(user.getPassword()));
        return userService.updateById(user)?Result.suc():Result.fail();
    }

    /**
     * 新增或更新
     * @param user
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody User user){
        //密码加密
        user.setPassword(Md5Util.md5Encryp(user.getPassword()));
        return userService.saveOrUpdate(user)?Result.suc():Result.fail();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        User user = userService.getById(id);
        if (ObjectUtil.isEmpty(user)){
            return Result.suc();
        }
        if (RoleEnum.SUPERADMIN.getCode().equals(user.getRoleId())){
            return Result.fail("超管不能删");
        }
        return userService.removeById(id)?Result.suc():Result.fail();
    }
}
